import axios from 'axios';
const USER_LIST = 'user_list';
const USER_LIST_ERR = 'user_list_err';

const store = {
    userListArr:[],//用户列表
    desc:''
}

export const userList = (state=store,action) => {
    switch(action.type) {
        case USER_LIST:
        return {...state,userListArr:action.paylaod,desc:''}
        case USER_LIST_ERR:
        return {...state,userListArr:[],desc:action.paylaod}
        default:
        return state
    }
}

function userListFun(data) {
    return {type:USER_LIST,paylaod:data}
}
function errMsg(desc) {
    return {type:USER_LIST_ERR,paylaod:desc};
}

export const userListAxios = (type) => {
    return (dispatch) => {
        axios.get(`/user/userlist?type=${type}`).then(res => {
            console.log(res);
            if(res.status === 200 && res.data.code === 0) {
                dispatch(userListFun(res.data.userlist));
            }else {
                dispatch(errMsg(res.data.desc));
            }
        })
    }
}